package com.gylang.print.pdf.test.docx;

import org.docx4j.jaxb.Context;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;

import java.io.File;
import java.util.HashMap;

/**
 * @author gylang
 * data 2021/6/2
 */
public class DocTemplateRender {

	public static void main(String[] args) throws Exception {

		org.docx4j.wml.ObjectFactory foo = Context.getWmlObjectFactory();

		String template = SampleDoc.BASE + "template.docx";

		boolean save = true;
		String outputfilepath = SampleDoc.BASE + "template-target.docx";

		WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File(template));
		MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();

		//需要替换的map
		HashMap<String, String> mappings = new HashMap<>();
		mappings.put("title1", "男");
		mappings.put("title2", "25");
		mappings.put("title3", "qlq");
		mappings.put("content", "content");

		long start = System.currentTimeMillis();

		documentPart.variableReplace(mappings);
		long end = System.currentTimeMillis();
		long total = end - start;
		System.out.println("Time: " + total);

		// Save it
		wordMLPackage.save(new File(outputfilepath));
	}
}
